"""扫描记录Service"""
from typing import List, Optional, Dict, Any
from sqlalchemy.orm import Session
from app.repositories.scan_record_repository import ScanRecordRepository
from app.models.scan_record import ScanRecord


class ScanRecordService:
    """扫描记录业务逻辑层"""

    def __init__(self, db: Session):
        self.repository = ScanRecordRepository(db)

    def get_all(
        self,
        skip: int = 0,
        limit: int = 100,
        filters: Optional[Dict[str, Any]] = None,
        order_by: Optional[str] = None,
    ) -> List[ScanRecord]:
        """获取扫描记录列表"""
        return self.repository.get_all(
            skip=skip, limit=limit, filters=filters, order_by=order_by
        )

    def get_by_id(self, record_id: int) -> Optional[ScanRecord]:
        """根据ID获取扫描记录"""
        return self.repository.get(record_id)

    def get_by_task_id(
        self,
        task_id: int,
        skip: int = 0,
        limit: int = 100,
        order_by: Optional[str] = None,
    ) -> List[ScanRecord]:
        """根据任务ID获取扫描记录列表"""
        return self.repository.get_by_task_id(
            task_id=task_id, skip=skip, limit=limit, order_by=order_by
        )

    def get_by_branch(
        self, branch: str, task_id: Optional[int] = None
    ) -> List[ScanRecord]:
        """根据分支获取扫描记录"""
        return self.repository.get_by_branch(branch=branch, task_id=task_id)

    def get_latest_by_task(self, task_id: int) -> Optional[ScanRecord]:
        """获取任务的最新扫描记录"""
        return self.repository.get_latest_by_task(task_id)

    def count(self, filters: Optional[Dict[str, Any]] = None) -> int:
        """统计扫描记录数"""
        return self.repository.count(filters=filters)

